<?php

namespace app\common\service;

/**
 * MobPush 服务
 */
class PushMobService
{
    /**
     * appKey
     * @var string
     */
    private $appKey;

    /**
     * appSecret
     * @var string
     */
    private $appSecret;

    /**
     * 推送地址
     */
    const URL_PUSH = "http://api.push.mob.com/v3/push/createPush";

    public function __construct()
    {
        $pushCfg = config('push');
        $this->appKey = $pushCfg['mobpush']['appKey'];
        $this->appSecret = $pushCfg['mobpush']['appSecret'];
    }

    /**
     * 通知推送(注册ID)
     * @param array $notification
     * @param array $registrationIds
     * @return bool|mixed|string
     */
    public function notificationByRegId($notification = [], $registrationIds = [])
    {
        $pushData = [
            'source' => 'webapi',
            'appkey' => $this->appKey,
            'pushTarget' => [
                'target' => 4,
                'rids' => $registrationIds
            ],
            'pushNotify' => [
                'plats' => [1,2],
                'title' => $notification['title'],
                'content' => $notification['content'],
                'type' => 1,
                'iosProduction' => $notification['apns_production'] ? 1 : 0,
                'extrasMapList' => [
                    [
                        'key' => 'event',
                        'value' => $notification['event']
                    ],
                    [
                        'key' => 'route',
                        'value' => $notification['route']
                    ]
                ],
                'taskCron' => 1,
                'taskTime' => (int)$notification['pushed_at'],
            ]
        ];
        $pushJson = json_encode( $pushData );

        $header = [
            'Content-Type: application/json; charset=utf-8',
            'key: '.$this->appKey,
            'sign: '.md5( $pushJson.$this->appSecret )
        ];

        return curl_post(self::URL_PUSH, $pushJson, $header);
    }

}